#pragma once
#include "growvector.h"
#include <map>
#include <string>

#ifndef null
#define null 0l
#endif

class Listener;
class	Switchboard{
public:
	std::map<const char*,growvector<Listener*>>				routing;
	void	unsubscribe(Listener* listener,const char* message){
		growvector<Listener*>&	lst=routing[message];
		lst.removeAll(listener);
	}
};

typedef struct{
	Switchboard*	board;
	const char*		message;
}Subscription;

class	Listener{
public:
	std::vector<Subscription>	subscriptions;
	void	unsubscribe(){
		for(unsigned int i=0;i<subscriptions.size();i++){
			Subscription& s=subscriptions[i];
			s.board->unsubscribe(this,s.message);
		}
		subscriptions.clear();
	}
	void	subscribe(Switchboard*	sb,const char*	msg){
		Subscription s={sb,msg};
		subscriptions.push_back(s);
	}
	static bool sendMessage(const char *msg=null,void*	params=null);
	static void	listen(Listener* rcvr,const char*	msg);
	virtual bool	message(const char *msg=null,void*	params=null);
	Listener();

	~Listener();
};
